package 复习.博客2.哈希;

public class 数组中出现次数超过一般的数字 {
    public int MoreThanHalfNum_Solution(int [] array) {
        int key = 0;
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if(key != array[i]){
                if(count == 0){
                    count = 1;
                    key = array[i];
                }else{
                    count--;
                }
            }else {
                count++;
            }
        }
        return key;
    }
}

/*
import java.util.*;
public class Solution {
    public int MoreThanHalfNum_Solution(int [] array) {
        int count = 0;
        int res = 0;
        for (int i = 0; i < array.length; i++) {
            if(count == 0){
                res = array[i];
                count++;
            }else if(res != array[i]){
                count--;
            }else{
                count++;
            }
        }
        return res;
    }
}
 */